import Vue from 'vue'
import Router from 'vue-router'
import {isLogin} from "@/api/users/users";

Vue.use(Router)

const router = new Router({
    mode: 'history',
    routes: [

        //这里咱们配置一个默认路由。
        {
            path: '/',
            name: 'Index',
            component: resolve => require.ensure([], () => resolve(require('@/views/IndexView')), 'Index'),
        },

        //用户登录视图组件
        {
            path: '/login',
            name: 'LoginView',
            component: resolve => require.ensure([], () => resolve(require('@/views/LoginView.vue')), 'LoginView'),
        },

        //以下都是空页面没有任何功能，仅仅是演示路由用的。
        {
            path: '/menu1',
            name: 'MenuView1',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuView1.vue')), 'MenuView1')
        },
        {
            path: '/menu3',
            name: 'MenuView3',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuView3.vue')), 'MenuView3')
        },
        {
            path: '/menu5',
            name: 'MenuView5',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuView5.vue')), 'MenuView5')
        },
        {
            path: '/menu2-1',
            name: 'MenuItemView2_1',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuItemView2_1.vue')), 'MenuItemView2_1')
        },
        {
            path: '/menu2-2',
            name: 'MenuItemView2_2',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuItemView2_2.vue')), 'MenuItemView2_2')
        },
        {
            path: '/menu2-3',
            name: 'MenuItemView2_3',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuItemView2_3.vue')), 'MenuItemView2_3')
        },
        {
            path: '/menu4-2',
            name: 'MenuItemView4_2',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuItemView4_2.vue')), 'MenuItemView4_2')
        },
        {
            path: '/menu4-3',
            name: 'MenuItemView4_3',
            component:resolve => require.ensure([], () => resolve(require('@/views/MenuItemView4_3.vue')), 'MenuItemView4_3')
        },

        //以下两路由是用来讲解sessionStorage
        {
            path: '/case/session',
            name: 'SessionView',
            component: resolve => require.ensure([], () => resolve(require('@/views/SessionView')), 'SessionView'),
            //component:()=>import("@/views/WelcomeView"),
        },
        {
            path: '/case/other',
            name: 'AnotherView',
            component: resolve => require.ensure([], () => resolve(require('@/views/AnotherView')), 'AnotherView'),
            //component:()=>import("@/views/WelcomeView"),
        },

        //这个用户中心页面，实现用户登录成功后修改用户资料
        {
            path: '/users/center',
            name: 'UsersCenterView',
            component: resolve => require.ensure([], () => resolve(require('@/views/UsersCenterView')), 'UsersCenterView'),
            //component:()=>import("@/views/WelcomeView"),
            meta: {
                requireAuth: true //说明这个路由必须检查登录权限...
            }
        }
    ]
})

//添加路由守卫
router.beforeEach((to, from, next) => {
    if (to.matched.some(record => record.meta.requireAuth)) {
        // 判断该路由是否需要登录权限
        //console.log('该路由需要检查登录权限....')
        //发送一个异步请求;
        isLogin().then(resp => {
            if (resp.data.code === 200) {
                next(); //放行.....
            } else {
                next({
                    path: '/login',
                    query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
                })
            }
        })
    } else {
        next();
    }
})


export default router;